package com.vhly.classfile;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class CPString
        extends CPEntry {

    private int string_index;
    public static final int tag = 8;

    public CPString() {
        string_index = 0;
    }

    public CPString(int i) {
        string_index = i;
    }

    public int getStringIndex() {
        return string_index;
    }

    public void setStringIndex(int i) {
        string_index = i;
    }

    public void write(DataOutputStream dataoutputstream)
            throws IOException {
        dataoutputstream.write(8);
        dataoutputstream.writeShort(string_index);
    }

    public void read(DataInputStream datainputstream)
            throws IOException {
        string_index = datainputstream.readUnsignedShort();
    }

    public int getType() {
        return 8;
    }

    /**
     * 比较指定元素是否和自身相等
     *
     * @param e CPEntry
     * @return 0 equals -1 no
     */
    public boolean equalsTo(CPEntry e) {
        CPString cpd = (CPString) e;
        return cpd.string_index == this.string_index;
    }
}